﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class OrtuFind : Form
    {
        private MuridAdd parent = null;
        private MuridEdit parent_edit = null;
        private OrtuAdd ortuadd = null;
        public DataTable dTable = new DataTable();

        public OrtuFind()
        {
            InitializeComponent();
        }

        public OrtuFind(MuridAdd parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        public OrtuFind(MuridEdit parent)
        {
            InitializeComponent();
            this.parent_edit = parent;
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            if (this.ortuadd == null || this.ortuadd.IsDisposed)
                ortuadd = new OrtuAdd(this);

            //check whether the form has been opened or not
            if (((Main)this.MdiParent).isOpenForm(ortuadd))
            {
                ortuadd = null;
                return;
            }

            ortuadd.MdiParent = this.MdiParent;
            ortuadd.Show();
        }

        private void lstNama_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRow[] data = this.dTable.Select("par_name='" + this.lstNama.SelectedItem + "'");

            if (data.Length == 1)
            {
                if (this.parent != null)
                {
                    // Get Parent ID
                    this.parent.par_id = (int)data[0].ItemArray[0];

                    this.txtNama.Text = data[0].ItemArray[1].ToString();
                    this.txtAlamat.Text = data[0].ItemArray[2].ToString();
                    this.txtPekerjaan.Text = data[0].ItemArray[3].ToString();
                }
                else if (this.parent_edit != null)
                {
                    // Get Parent ID
                    this.parent_edit.par_id = (int)data[0].ItemArray[0];

                    this.txtNama.Text = data[0].ItemArray[1].ToString();
                    this.txtAlamat.Text = data[0].ItemArray[2].ToString();
                    this.txtPekerjaan.Text = data[0].ItemArray[3].ToString();
                }
            }
        }

        private void OrtuFind_Load(object sender, EventArgs e)
        {
            dTable = Database.getInstance().query("select * from Parent");

            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                this.lstNama.Items.Add(dTable.Rows[i].ItemArray[1].ToString());
            }

            this.lstNama.Sorted = true;
        }

        private void btnPilih_Click(object sender, EventArgs e)
        {
            if (this.parent != null)
            {
                this.parent.txtOrtu.Text = this.txtNama.Text;
                this.parent.txtAlamat.Text = this.txtAlamat.Text;
                this.parent.txtPekerjaan.Text = this.txtPekerjaan.Text;
            }
            else if (this.parent_edit != null)
            {
                this.parent_edit.txtOrtu.Text = this.txtNama.Text;
                this.parent_edit.txtAlamat.Text = this.txtAlamat.Text;
                this.parent_edit.txtPekerjaan.Text = this.txtPekerjaan.Text;
            }
         
            this.Close();
        }
    }
}
